<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
import { statisticFollow } from '@/api/research/follow'

require('echarts/theme/cool') // echarts theme

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      virtulData: []
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  created() {
    this.statisticFollow()
  },
  methods: {
    statisticFollow() {
      statisticFollow().then(response => {
        const keys = Object.keys(response.data)
        if (keys.length === 0) {
          this.initChart()
          return false
        }
        let data = []
        keys.forEach(item => {
          data.push([item, response.data[item]])
        })
        this.virtulData = data
        this.initChart()
      })
    },
    getCurrentYearMonth() {
      let nowDate = new Date()
      return nowDate.getFullYear() + '-' + (nowDate.getMonth() + 1)
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'cool')
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        title: {
          text: '随访日历'
        },
        tooltip: {
          position: 'top',
          formatter: function(params) {
            let htmlStr = '<div>'
            htmlStr += '待随访 ' + params.value[1]
            htmlStr += '</div>'

            return htmlStr
          }
        },

        calendar: [{
          orient: 'vertical',
          yearLabel: {
            margin: 40
          },
          monthLabel: {
            nameMap: 'cn',
            margin: 20
          },
          dayLabel: {
            nameMap: 'cn'
          },
          left: 'center',
          cellSize: 40,
          range: this.getCurrentYearMonth()
        }],

        series: [{
          type: 'effectScatter',
          coordinateSystem: 'calendar',
          calendarIndex: 0,
          symbolSize: function(val) {
            return val[1] * 5
          },
          data: this.virtulData
        }]
      })
    }
  }
}
</script>
